﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Mail;
using System.Net;
//using BusinessObjects;
using Entities;

namespace RMSoftware.Models
{
    public class DataManager
    {
        private RMDatabaseEntities _db;

        public DataManager()
        {
            _db = new RMDatabaseEntities();
        }

        public IQueryable<RiskFactor> RiskFactors
        {
            get
            {
                return _db.RiskFactors;
            }
        }

        public IQueryable<Person> Persons
        {
            get
            {
                return _db.Persons;
            }
        }

        public IQueryable<Project> Projects
        {
            get
            {
                return _db.Projects;
            }
        }

        public Project GetProject(Guid id)
        {
            return _db.Projects.SingleOrDefault(proj => proj.ProjectId == id);
        }

        public void SaveProject(Project obj)
        {
            Project old = GetProject(obj.ProjectId);

            if (old == null)
            {
                _db.Projects.AddObject(obj);
            }
            else
            {
                old.Title = obj.Title;
            }

            _db.SaveChanges();
        }

        public void DeleteProject(Guid id)
        {
            DeleteProject(GetProject(id));
        }

        public void DeleteProject(Project obj)
        {
            while (obj.Risks.Count != 0)
            {
                DeleteRisk(obj.Risks.First());
            }

            _db.Projects.DeleteObject(obj);
            _db.SaveChanges();
        }

        public RiskFactor GetRiskFactor(Guid id)
        {
            return _db.RiskFactors.SingleOrDefault(fact => fact.RiskId == id);
        }

        public void AddRiskFactor(RiskFactor obj)
        {
            _db.RiskFactors.AddObject(obj);
            _db.SaveChanges();
        }

        public void DeleteRiskFactor(Guid id)
        {
            _db.RiskFactors.DeleteObject(GetRiskFactor(id));
            _db.SaveChanges();
        }

        public Person GetPerson(Guid id)
        {
            return _db.Persons.SingleOrDefault(pers => pers.PersonId == id);
        }

        public void AddPerson(Person obj)
        {
            _db.Persons.AddObject(obj);
            _db.SaveChanges();
        }

        public void DeletePerson(Guid id)
        {
            _db.Persons.DeleteObject(GetPerson(id));
            _db.SaveChanges();
        }

        public Risk GetRisk(Guid id)
        {
            return _db.Risks.SingleOrDefault(pr => pr.ProjectRiskId == id);
        }

        public void SaveRisk(Risk obj)
        {
            Risk old = GetRisk(obj.ProjectRiskId);

            if (old == null)
            {
                _db.Risks.AddObject(obj);
            }
            else
            {
                old.Probability = obj.Probability;
                old.Cost = obj.Cost;
            }

            _db.SaveChanges();
        }

        public void DeleteRisk(Guid id)
        {
            DeleteRisk(GetRisk(id));
        }

        public void DeleteRisk(Risk obj)
        {
            while (obj.Measures.Count != 0)
            {
                DeleteMeasure(obj.Measures.First());
            }

            _db.Risks.DeleteObject(obj);
            _db.SaveChanges();
        }

        public Measure GetMeasure(Guid id)
        {
            return _db.Measures.SingleOrDefault(m => m.MeasureId == id);
        }

        public void DeleteMeasure(Measure obj)
        {
            _db.Measures.DeleteObject(obj);
            _db.SaveChanges();
        }

        public void AddMeasure(Measure obj)
        {
            _db.Measures.AddObject(obj);
            _db.SaveChanges();
        }

        // TODO: переписать
        public IQueryable<Measure> GetPersonMeasures(Guid personId)
        {
            List<Measure> result = new List<Measure>();

            foreach (Measure measure in _db.Measures)
            {
                if (measure.PersonId == personId)
                {
                    result.Add(measure);
                }
            }

            return result.AsQueryable();
        }

        public void SaveMeasure(Measure obj)
        {
            Measure old = GetMeasure(obj.MeasureId);

            if (old == null)
            {
                _db.Measures.AddObject(obj);
            }
            else
            {
                old.ReqBegDate = obj.ReqBegDate;
                old.ReqEndDate = obj.ReqEndDate;
                old.BegDate = obj.BegDate;
                old.EndDate = obj.EndDate;
            }

            _db.SaveChanges();
        }

        public Guid GetPersonIdByUsername(string username)
        {
            return _db.Persons.SingleOrDefault(p => p.Username == username).PersonId;
        }

        public void SaveRiskFactor(RiskFactor obj)
        {
            RiskFactor old = GetRiskFactor(obj.RiskId);

            if (old == null)
            {
                _db.RiskFactors.AddObject(obj);
            }
            else
            {
                old.Title = obj.Title;
                old.Description = obj.Description;
            }

            _db.SaveChanges();
        }

        public void SendEmailThroughGmail(string subject, string messageBody, string emailTo)
        {
            SmtpClient client = new SmtpClient();
            NetworkCredential basicAuthenticationInfo =
                new NetworkCredential("rmsubsystem@gmail.com", "rmsubsystem");
            client.Host = "smtp.gmail.com";
            client.UseDefaultCredentials = false;
            client.Credentials = basicAuthenticationInfo;
            client.EnableSsl = true;

            MailAddress to = new MailAddress(emailTo);
            MailAddress from = new MailAddress("rmsubsystem@gmail.com", "RMSubsystem", System.Text.Encoding.UTF8);

            MailMessage message = new MailMessage(from, to);
            message.Body = messageBody;
            message.IsBodyHtml = true;
            message.BodyEncoding = System.Text.Encoding.UTF8;
            message.Subject = subject;
            message.SubjectEncoding = System.Text.Encoding.UTF8;

            client.Send(message);
        }

    }
}